<?php
class omekpi_ctl_admin_servicer extends desktop_controller{
    var $name = "客服绩效";
    var $workground = "performance";
    
    function index(){
        $servicer_log = $this->app->model('servicer_log');
//      $this->getOrderIncremental();
        $left = app::get('omekpi')->getConf('system.left_mark');
        $right = app::get('omekpi')->getConf('system.right_mark');
        if($left){
            $this->pagedata['left'] = $left;
        }else{
            $this->pagedata['left'] = '[-';
        }
        if($right){
            $this->pagedata['right'] = $right;
        }else{
            $this->pagedata['right'] = '-]';
        }
        $this->pagedata['begin_date'] = $_POST['begin_date'];
        $this->pagedata['end_date'] = $_POST['end_date'];
        $omekpi = $servicer_log->getPageData();
        $count = $servicer_log->getCount();
        $this->pagedata['count']=$count;
        
        $this->pagedata['slog_List'] = $omekpi;
        $this->pagedata['csv_data'] = serialize($omekpi);
        $this->page('admin/performance/servicer_log.html');
    }
    
    function getPagedata($sList=array(),$slog_List=array()){
        $omekpi=array();
        foreach($sList as $ke=>$va){//人员列表
            foreach($slog_List as $k=>$v){//绩效列表
                if($v['employee_id'] == $va['employee_id']){ 
                    $array['employee_id'] =$v['employee_id'];
                    $array['employee_name'] = $va['employee_name'];
                    $array['employee_op_no'] = $va['employee_op_no'];
                    $array['received_orders'] = $v['received_orders'];
                    $array['payed_orders'] = $v['payed_orders'];
                    $array['my_sales'] = $v['my_sales'];
                    $array['createdate'] = $v['createdate'];
                    array_push($omekpi,$array);
                    $array = null;
                }
            }
        }
        return $omekpi;
    }
/*
    function getOrderIncremental($order = array()){//invoice , 更新servicer_log表 
      	if(!$order) return false;
        $ser_array= array();
        $addarray = array();
        $name = $this->getName($order['mark_text']);//帐户名称
        $ser_array['createdate'] = date('Ymd',$order['createtime']);
        //pay_status 是否支付(0 未支付 1 已支付 2 处理中 3 部分付款 4 部分退款 5 全额退款)
        if($order['pay_status']==1 or $order['pay_status']==3 or $order['pay_status']==4){
            $ser_array['my_sales']  = $order['payed'];
            $ser_array['payed_orders'] = 1;
        }else{
            $ser_array['my_sales']  = 0;
            $ser_array['payed_orders'] = 0;
        }
        $ser_array['received_orders'] = 1;
        $employee = &$this->app->model('employee');//员工列表
        $servicer_log = &$this->app->model('servicer_log');//客服人员绩效表
        if($name){//没名字servicer_id = 0;
            $em_filter=array('employee_login_name'=>$name);
            $r = $employee->dump($em_filter,'employee_id');
            if(!$r){
                $addarray['employee_id'] = 0;
                $ser_array['employee_id'] = 0;
            }else{
                $addarray['employee_id'] = $r['employee_id'];
                $ser_array['employee_id'] = $r['employee_id'];
            }
        }else{
            $addarray['employee_id'] = 0;
            $ser_array['employee_id'] = 0;
        }
        $res = $servicer_log->dump(array('employee_id'=>$addarray['employee_id'],'createdate'=>$ser_array['createdate']));
        if($res){
            
                $addarray['createdate'] = $ser_array['createdate'];
                $addarray['my_sales'] = $ser_array['my_sales']+$res['my_sales'];
                $addarray['payed_orders'] = $ser_array['payed_orders']+$res['payed_orders'];
                $addarray['received_orders'] = $ser_array['received_orders']+$res['received_orders'];
            $servicer_log->update($addarray);
        }else{
            $servicer_log->insert($ser_array);
        }
        $invoice =&$this->app->model('servicer_invoice');
        $invoice_data = array('employee_id'=>$ser_array['employee_id'],'order_id'=>$order['order_id']);
        $invoice->insert($invoice_data);
    } 
*/
    function getName($des){
        $o = $this->getCfg();
        if($o){
            $first = strrpos($des,$o[0]);
            $second = strrpos($des,$o[1]);
        }
        if($first == false || $second == false){
             return null;
        }else{
            $third = $second - $first;
            $name = substr($des,$first+2,$third-2);  //最后得到接单人的名字
            return $name;        
        }
    }

    //查看单人的已接定单
    function getOrders(){
        $employee_id = $_GET['id'];
        $createdate = $_GET['date'];
        $servicer_invoice = $this->app->model('servicer_invoice');
        $order_ids =  $servicer_invoice->getList('order_id',array('employee_id'=>$employee_id),0,-1);
        $o_ids = array();
        if($order_ids) {
            foreach($order_ids as $v){
                array_push($o_ids,$v['order_id']);
            }
            $filter['createtime'] = strtotime($createdate);
        }

        $filter['order_id']=$o_ids;
        
        $params = array(
                'title'=>'客服已接定单',
                'use_buildin_new_dialog' => false,
                'use_buildin_set_tag'=>false,
                'use_buildin_recycle'=>false,
                'use_buildin_export'=>false,
                'use_buildin_import'=>false,
                'use_buildin_filter'=>true,
                'base_filter' => $filter,
                'finder_aliasname'=>'order_view',
                'finder_cols'=>'createtime,shop_id,ship_area,is_cod,total_amount,payment,shipping,mark_type,mark_text',
            );
        $this->finder('ome_mdl_orders',$params);
    }
        //查看单人的已付款定单
    function getPayedOrders(){

        $employee_id = $_GET['id'];
        $time = strtotime($_GET['date']);
        $createdate=date('Ymd',strtotime($_GET['date']));
        $servicer_invoice = $this->app->model('servicer_invoice');
        $order_ids =  $servicer_invoice->getList('order_id',array('employee_id'=>$employee_id,'createdate'=>$createdate),0,-1);
        $o_ids = array();
        if($order_ids) {
            foreach($order_ids as $v){
                array_push($o_ids,$v['order_id']);
            }
            $filter['createtime'] = $time;
            $filter['pay_status']=array(1,3,4);
        }
        
        $filter['order_id']=$o_ids;
        $params = array(
                'title'=>'客服已支付定单',
                'use_buildin_new_dialog' => false,
                'use_buildin_set_tag'=>false,
                'use_buildin_recycle'=>false,
                'use_buildin_export'=>false,
                'use_buildin_import'=>false,
                'use_buildin_filter'=>true,
                'base_filter' => $filter,
                'finder_aliasname'=>'order_view',
                'finder_cols'=>'createtime,shop_id,ship_area,is_cod,total_amount,payment,shipping,mark_type',
            );
        $this->finder('ome_mdl_orders',$params);
    }

//查询
    function search(){
        $this->begin('index.php?app=omekpi&ctl=admin_servicer&act=index');
        $servicer_log = &$this->app->model('servicer_log');
        $employee = $this->app->model('employee');
        if (!$_POST['begin_date']&&!$_POST['end_date']&&!$_POST['have']){
            $this->end(true,'搜索完成');
        }

        $begin_date = date('Ymd',strtotime($_POST['begin_date']));
        $end_date = date('Ymd',strtotime($_POST['end_date']));
        if($_POST['begin_date'] && $_POST['end_date'] && ($begin_date > $end_date)){
            $this->end(false,'开始时间大于结束时间');
        }

        if (!$_POST['have']){//如果没有名字
            $where = 'where 1 ';
            if($_POST['begin_date']){
            $where .= "and createdate >= '".$begin_date."'";
            }
            if($_POST['end_date']){
                $where .= "and createdate <= '".$end_date."'";
            }
            $slogList = $servicer_log->search($where);
            $em_filter = array('employee_type'=>'servicer'); 
            $sList = $employee->getList('*',$em_filter,0,-1);
            $searchList = $this->getPagedata($sList,$slogList);
            if(!$searchList){
                $this->end(true,'无当前条件结果');
            }
            $count = $servicer_log->getCount($where);
        }else{
            $filter=array();
            if($_POST['select']=='NA' ){
                $filter['employee_name'] = $_POST['have'];
            }else{
                if($_POST['select']=='NO' ){
                    $filter['employee_op_no'] = $_POST['have'];
                }
            }
            $filter['employee_type'] = 'servicer'; 
            $sList = $employee->getList('*',$filter,0,-1);
            $where = ' where 1 ';
            $in = '';
            if($sList){
                foreach($sList as $v){
                    $in .=$v['employee_id'].',';
                }
                $where .=' and employee_id in ('.substr($in,0,strlen($in)-1).')';
            }else{
                $this->end(false,$_POST['have'].'不在客服列表中');
            }
            if($_POST['begin_date']){
                $where .= " and createdate >= '".$begin_date."'";
            }
            
            if($_POST['end_date']){
                $where .= " and createdate <= '".$end_date."'";
            }
            $slogList = $servicer_log->search($where);
            $searchList = $this->getPagedata($sList,$slogList);
            if(!$searchList){
                $this->end(true,'无当前条件结果');
            }
            $count = $servicer_log->getCount($where);
        }
        $this->pagedata['count']=$count;
        $this->pagedate['employee_op_no']=$_POST['employee_op_no'];
        $this->pagedata['employee_name']=$_POST['employee_name'];
        $this->pagedata['begin_date'] = $_POST['begin_date'];
        $this->pagedata['end_date'] = $_POST['end_date'];
        $this->pagedata['slog_List'] = $searchList;
        $this->pagedata['csv_data'] = serialize($searchList);
        $this->page('admin/performance/servicer_log.html');
    }
//导出
    function export(){
        $this->begin("index.php#app=omekpi&ctl=admin_servicer&act=index");
        $mess = '员工编号,员工姓名,已接订单数量,已付款订单数量,销售总额,日期';
        $res = unserialize(stripcslashes($_POST['csv_data']));
        if($res){
            foreach($res as $value){
                $mess .= "\r".$value['employee_op_no'].",".
                         $value['employee_name'].",".
                         $value['received_orders'].",".
                         $value['payed_orders'].",".
                         $value['my_sales'].",".
                         $value['createdate']."\r";
            }
        }
        header("Content-type: application/octet-stream");
        header("Accept-Ranges:bytes");
        $filename = '客服绩效'.date('m月d日',time()).".csv";
        $encoded_filename = urlencode($filename);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        
        $ua = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }
        //header("Content-Disposition: attachment; filename=".'客服绩效'.date('m月d日',time()).".csv");
        echo mb_convert_encoding($mess,"GBK","UTF-8");
    }
    
//导入    
    function import(){
        $this->begin("index.php#app=omekpi&ctl=admin_performance_service&act=index");
        $file = $_FILES['package'];
        $extname = strtolower($this->extName($file['name']));
        $service = &$this->app->model('performance_service');
        if($extname=='.csv'){
            $content = file_get_contents($file['tmp_name']);
            $array = explode("\r",$content);
            array_pop($array);
            foreach($array as $value){
                $ar = explode(',',$value);
                $tmp['cust_name'] = $ar[0];
                $tmp['received_orders'] = $ar[1];
                $tmp['payed_orders'] = $ar[2];
                $tmp['my_sales'] = $ar[3];
                $tmp['createdate'] = $ar[4];

                $abc=$service->save($tmp);
                $tmp = null;
            }
            $this->end(true,'上传完成');
        }else{
            $this->end(false,'必须是csv文件');
        }
    }
    
    
//文件后缀名
    function extName($file){
        return substr($file,strrpos($file,'.'));
    }

//用户自定义客服人员前后缀
    function setCfg(){
        $content = $_POST['start'].','.$_POST['end'];
        $file = fopen($this->app->app_dir.'/cfg.txt','w');
        if ($file && fwrite($file,$content))
        {
            fclose($file);
            return true;
        }
        fclose($file);
        return false;
        
    }
    function getCfg(){
        try{
            $content = file_get_contents($this->app->app_dir.'/cfg.txt');
            if($content){
                return split(',',$content);
            }
            return array(0=>'[-',1=>'-]');
        }catch(Exception $e){
            return array(0=>'[-',1=>'-]');
        }
        
    }
    
    
    
    function setmark(){
        $this->begin();
        if(empty($_POST['left_mark']) || empty($_POST['right_mark'])){
            $this->end(false,"标签不能为空");
        }else{
            app::get('omekpi')->setConf('system.left_mark',$_POST['left_mark']);
            app::get('omekpi')->setConf('system.right_mark',$_POST['right_mark']);
            $left = app::get('omekpi')->getConf('system.left_mark');
            $right = app::get('omekpi')->getConf('system.right_mark');
            if(isset($left) && isset($right)){
                $this->end(true,"标签设置成功。");
            }else{
                $this->end(false,"标签设置失败。");
            }
        }
        
    }

}
?>
